package com.seanLab.service;

import com.seanLab.domain.*;
import com.seanLab.dto.FeedbackDto.*;
import com.seanLab.repository.*;
import com.seanLab.tool.validator.FeedbackDtoValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by ailly on 17-9-22.
 */
@Component
public class FeedbackService {
    @Autowired
    private EditorFeedbackRepo editorFeedbackRepo;

    @Autowired
    private EditorAddTagRepo editorAddTagRepo;

    @Autowired
    private EditorDeleteTagRepo editorDeleteTagRepo;

    @Autowired
    private EditorImageScoreRepo editorImageScoreRepo;

    @Autowired
    private FeedbackDtoValidator fv;

    public Body addFeedback(EditorFeedbackDto editorFeedbackDto){
        //TODO 检测Dto完整性：sessionID,imgID等

        int errorType = fv.validateFeedback(editorFeedbackDto);
        if (errorType == 0) {
            EditorFeedback editorFeedback = FeedbackDto2Domain.transferEditorFeedback(editorFeedbackDto);
            this.editorFeedbackRepo.save(editorFeedback);
            return Body.getResponseBody(errorType);
        } else {
            return Body.getResponseBody(errorType);
        }

    }

    //增加某一图片标签
    public Body addTag(EditorAddTagDto editorAddTagDto) {
        int errorType = fv.validateAddTag(editorAddTagDto);

        if (errorType == 0) {
            EditorAddTag editorAddTag = FeedbackDto2Domain.transferEditorAddTag(editorAddTagDto);
            this.editorAddTagRepo.save(editorAddTag);
            return Body.getResponseBody(errorType);
        } else {
            return Body.getResponseBody(errorType);
        }
    }

    //删除某一图片标签
    public Body deleteTag(EditorDeleteTagDto editorDeleteTagDto) {
        int errorType = fv.validateDeleteTag(editorDeleteTagDto);

        if (errorType == 0) {
            EditorDeleteTag editorDeleteTag = FeedbackDto2Domain.transferEditorDeleteTag(editorDeleteTagDto);
            //TODO 检测 editorDeleteTag 完整性
            this.editorDeleteTagRepo.save(editorDeleteTag);
            return Body.getResponseBody(errorType);
        } else {
            return Body.getResponseBody(errorType);
        }
    }

    //给多个图片评分
    public Body addImgScore(EditorImageScoreDto editorImageScoreDto) {
        int errorType = fv.validateImageScore(editorImageScoreDto);

        if (errorType == 0) {
            List<EditorImageScore> editorImageScoreList = FeedbackDto2Domain.transferEditorImageScore(editorImageScoreDto);
            //TODO 检测 editorImageScoreList 完整性
            this.editorImageScoreRepo.save(editorImageScoreList);
            return Body.getResponseBody(errorType);
        } else {
            return Body.getResponseBody(errorType);
        }

    }
}
